package com.aidex.biz.smy.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.util.encoders.Base64;

import java.util.Map;
import java.util.TreeMap;

/**
 * Created by wangjianqiang on 2018/1/8.
 * 加密方：原文->求MD5->RSA私钥加密->base64编码字符串（密文）
 * 解密方：密文->base64解码->RSA公钥钥解密->原始MD5
 *
 * @return
 */
public class RSASignUtil {
    private static Logger logger = LoggerFactory.getLogger(RSASignUtil.class);

    /**
     * 公钥解密
     *
     * @param encryptSign
     * @param publicKey
     * @return
     */
    public static String decryptSign(String encryptSign, String publicKey) {
        try {
            logger.info("签名解密处理开始");

            byte[] signBase64 = Base64.decode(encryptSign);
            String sign = new String(RSAUtil.decryptByPublicKey(signBase64, publicKey));
            
            logger.info("签名解密处理结束");
            return sign;
        } catch (Exception e) {
            throw new RuntimeException("签名解密异常");
        }
    }

    public static String decryptSignPriKey(String encryptSign, String privateKey) {
        try {
            logger.info("签名解密处理开始");

            byte[] signBase64 = Base64.decode(encryptSign);
            String sign = new String(RSAUtil.decryptByPrivateKey(signBase64, privateKey));

            logger.info("签名解密处理结束");
            return sign;
        } catch (Exception e) {
            throw new RuntimeException("签名解密异常");
        }
    }

    /**
     * 私钥加密
     *
     * @param sign
     * @return
     * @throws Exception
     */
    public static String encryptSign(String sign, String privateKey) {
        try {
//            String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDELGWoGUpLIclSDrHr6qOGG4KaTpvZB/XPP2CWZTWaW3wnqD6XiOgCbL2iJm33cXZQuVNwLS7CSStIP6k5+h0dwoHMwwZVs+IZy8wlzsSpLamAfKXiG+wKphOJP1qowSEbV5qXukYVoEoaqR9xDZRBCOvMCWIi00vGrUwdJMOIPwIDAQAB";
            byte[] origiSignPublicKeyEncrypt = RSAUtil.encryptByPrivateKey(sign.getBytes(), privateKey);
            byte[] origiSignBase64 = Base64.encode(origiSignPublicKeyEncrypt);
            String encryptSign = new String(origiSignBase64);
            return encryptSign;
        } catch (Exception e) {
            throw new RuntimeException("签名加密异常");
        }
    }
    /**
     * 私钥加密
     *
     * @param sign
     * @return
     * @throws Exception
     */
    public static String encryptSignPubKey(String sign, String pubKey) {
        try {
//            String publicKey="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDELGWoGUpLIclSDrHr6qOGG4KaTpvZB/XPP2CWZTWaW3wnqD6XiOgCbL2iJm33cXZQuVNwLS7CSStIP6k5+h0dwoHMwwZVs+IZy8wlzsSpLamAfKXiG+wKphOJP1qowSEbV5qXukYVoEoaqR9xDZRBCOvMCWIi00vGrUwdJMOIPwIDAQAB";
            byte[] origiSignPublicKeyEncrypt = RSAUtil.encryptByPublicKey(sign.getBytes(), pubKey);
            byte[] origiSignBase64 = Base64.encode(origiSignPublicKeyEncrypt);
            String encryptSign = new String(origiSignBase64);
            return encryptSign;
        } catch (Exception e) {
            throw new RuntimeException("签名加密异常");
        }
    }

    public static void main(String[] args) throws Exception {

    }
    public static String getSortString( Map<String,String> param){
        StringBuilder paramStr = new StringBuilder();

        Map<String, String> paramSort = new TreeMap<>();

        //默认排序，按照ASCII升序
        for (Map.Entry<String, String> entry : param.entrySet()) {
            paramSort.put(entry.getKey(), entry.getValue());
        }

        // 拼接字符串
        for (Map.Entry<String, String> entry : paramSort.entrySet()) {
            paramStr.append("&").append(entry.getKey()).append("=").append(entry.getValue());
        }
        if (paramStr.length() > 0) {
            paramStr.deleteCharAt(0);
        }

        return paramStr.toString();
    }
}
